package com.peas.platform.module.icims.expenditure.kpis.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peas.platform.common.core.dto.ResponseDto;
import com.peas.platform.framework.mybatisplus.utils.PageUtil;
import com.peas.platform.module.icims.expenditure.kpis.api.req.*;
import com.peas.platform.module.icims.expenditure.kpis.api.resp.QueryExpenditureKpiResp;
import com.peas.platform.module.icims.expenditure.kpis.controller.criteria.ExpenditureKpiCriteria;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureAdjustmentDto;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureKpiDto;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureQuotaDto;
import com.peas.platform.module.icims.expenditure.kpis.service.ExpenditureAdjustmentService;
import com.peas.platform.module.icims.expenditure.kpis.service.ExpenditureKpiService;
import com.peas.platform.module.icims.expenditure.kpis.service.ExpenditureQuotaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;

@Api(tags = "自定义接口-[收支控制]指标单")
@RestController
@RequestMapping("/api/v1/customExpenditureKpis")
public class CustomExpenditureKpisController {

    @Resource
    private ExpenditureKpiService expenditureKpiService;
    @Resource
    private ExpenditureQuotaService expenditureQuotaService;
    @Resource
    private ExpenditureAdjustmentService expenditureAdjustmentService;

    @ApiOperation(value = "查询指标单可用金额")
    @GetMapping(value = "/queryExpenditureKpiAvailableAmount")
    public ResponseDto<BigDecimal> queryExpenditureKpiAvailableAmount(@RequestParam(value = "expenditureKpiId") Long expenditureKpiId,
                                                                      @RequestParam(value = "expenditureEconomicId", required = false) String expenditureEconomicId,
                                                                      @RequestParam(value = "useFluctuateStr", required = false) String useFluctuateStr) {
        Assert.notNull(expenditureKpiId, "指标单ID不能为空");
        if (StrUtil.isBlank(expenditureEconomicId)) {
            return ResponseDto.getSuccessResponseDto(expenditureKpiService.queryAvailableAmount(expenditureKpiId));
        }
        Assert.notNull(useFluctuateStr, "参数错误");
        String[] yes = {
                "true",
                "yes",
                "y",
                "1",
                "enable",
        };
        boolean useFluctuate = ArrayUtil.contains(yes, useFluctuateStr.toLowerCase());
        return ResponseDto.getSuccessResponseDto(expenditureKpiService.queryAvailableAmount(expenditureKpiId, expenditureEconomicId, useFluctuate));
    }

    @ApiOperation(value = "查询指标单（包含调整金额、下达单金额、申请金额、报账金额、可用金额）")
    @GetMapping(value = "/queryExpenditureKpi/{expenditureKpiId}")
    public ResponseDto<QueryExpenditureKpiResp> queryExpenditureKpi(@PathVariable(value = "expenditureKpiId") Long expenditureKpiId) {
        ExpenditureKpiDto dto = expenditureKpiService.getById(expenditureKpiId);
        if (dto == null) {
            return ResponseDto.getSuccessResponseDto(null);
        }
        QueryExpenditureKpiResp kpi = BeanUtil.copyProperties(dto, QueryExpenditureKpiResp.class);
        // 总调整金额
        kpi.setTotalAdjustmentAmount(expenditureKpiService.getTotalAdjustmentAmount(kpi.getId()));
        // 总下达金额
        kpi.setTotalQuotaAmount(expenditureKpiService.getTotalQuotaAmount(kpi.getId()));
        // 总申请金额
        kpi.setTotalApplicationAmount(expenditureKpiService.getTotalApplicationAmount(kpi.getId()));
        // 总报账金额
        kpi.setTotalReimbursementAmount(expenditureKpiService.getTotalReimbursementAmount(kpi.getId()));
        // 可用金额
        kpi.setAvailableAmount(expenditureKpiService.queryAvailableAmount(kpi.getId()));
        return ResponseDto.getSuccessResponseDto(kpi);
    }

    @ApiOperation(value = "查询指标单（包含调整金额、下达单金额、申请金额、报账金额、可用金额），返回分页")
    @GetMapping(value = "/queryExpenditureKpi")
    public ResponseDto<Page<QueryExpenditureKpiResp>> queryExpenditureKpi(ExpenditureKpiCriteria condition,
                                                                          @ApiParam(name = "pageNum", value = "分页页码", example = "1") Integer pageNum,
                                                                          @ApiParam(name = "pageSize", value = "分页每页大小", example = "10") Integer pageSize,
                                                                          @ApiParam(name = "sortField", value = "排序字段", example = "id") String sortField,
                                                                          @ApiParam(name = "sortType", value = "排序方式", example = "desc") String sortType) {
        Page<ExpenditureKpiDto> pager = PageUtil.genPager(pageNum, pageSize, sortField, sortType);
        ExpenditureKpiDto c = BeanUtil.copyProperties(condition, ExpenditureKpiDto.class);
        Page<ExpenditureKpiDto> page = expenditureKpiService.page(pager, c);
        Page<QueryExpenditureKpiResp> vos = PageUtil.copyPage(page, QueryExpenditureKpiResp.class);
        if (CollUtil.isNotEmpty(vos.getRecords())) {
            vos.getRecords().forEach(kpi -> {
                // 总调整金额
                kpi.setTotalAdjustmentAmount(expenditureKpiService.getTotalAdjustmentAmount(kpi.getId()));
                // 总下达金额
                kpi.setTotalQuotaAmount(expenditureKpiService.getTotalQuotaAmount(kpi.getId()));
                // 总申请金额
                kpi.setTotalApplicationAmount(expenditureKpiService.getTotalApplicationAmount(kpi.getId()));
                // 总报账金额
                kpi.setTotalReimbursementAmount(expenditureKpiService.getTotalReimbursementAmount(kpi.getId()));
                // 可用金额
                kpi.setAvailableAmount(expenditureKpiService.queryAvailableAmount(kpi.getId()));
            });
        }
        return ResponseDto.getSuccessResponseDto(vos);
    }

    @ApiOperation(value = "编辑保存指标单")
    @PostMapping(value = "/saveExpenditureKpi")
    public ResponseDto<ExpenditureKpiDto> saveExpenditureKpi(@RequestBody @Valid SaveExpenditureKpiVo vo) {
        ExpenditureKpiDto dto = expenditureKpiService.saveExpenditureKpi(vo);
        return ResponseDto.getSuccessResponseDto(dto);
    }

    @ApiOperation(value = "编辑提交指标单")
    @PostMapping("/submitExpenditureKpi")
    public ResponseDto<Boolean> submitExpenditureKpi(@RequestBody @Valid SubmitExpenditureKpiVo vo) {
        expenditureKpiService.submitExpenditureKpi(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "撤回指标单")
    @PostMapping("/revocationExpenditureKpi/{id}")
    public ResponseDto<Boolean> revocationExpenditureKpi(@PathVariable("id") Long id) {
        expenditureKpiService.revocationExpenditureKpi(id);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "编辑保存额度下达单")
    @PostMapping(value = "/saveExpenditureQuota")
    public ResponseDto<ExpenditureQuotaDto> saveExpenditureQuota(@RequestBody @Valid SaveExpenditureQuotaVo vo) {
        ExpenditureQuotaDto dto = expenditureQuotaService.saveExpenditureQuota(vo);
        return ResponseDto.getSuccessResponseDto(dto);
    }

    @ApiOperation(value = "编辑提交额度下达单")
    @PostMapping("/submitExpenditureQuota")
    public ResponseDto<Boolean> submitExpenditureQuota(@RequestBody @Valid SubmitExpenditureQuotaVo vo) {
        expenditureQuotaService.submitExpenditureQuota(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "撤回额度下达单")
    @PostMapping("/revocationExpenditureQuota/{id}")
    public ResponseDto<Boolean> revocationExpenditureQuota(@PathVariable("id") Long id) {
        expenditureQuotaService.revocationExpenditureQuota(id);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "编辑保存调整单")
    @PostMapping(value = "/saveExpenditureAdjustment")
    public ResponseDto<ExpenditureAdjustmentDto> saveExpenditureAdjustment(@RequestBody @Valid SaveExpenditureAdjustmentVo vo) {
        ExpenditureAdjustmentDto dto = expenditureAdjustmentService.saveExpenditureAdjustment(vo);
        return ResponseDto.getSuccessResponseDto(dto);
    }

    @ApiOperation(value = "编辑提交调整单")
    @PostMapping("/submitExpenditureAdjustment")
    public ResponseDto<Boolean> submitExpenditureAdjustment(@RequestBody @Valid SubmitExpenditureAdjustmentVo vo) {
        expenditureAdjustmentService.submitExpenditureAdjustment(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "撤回调整单")
    @PostMapping("/revocationExpenditureAdjustment/{id}")
    public ResponseDto<Boolean> revocationExpenditureAdjustment(@PathVariable("id") Long id) {
        expenditureAdjustmentService.revocationExpenditureAdjustment(id);
        return ResponseDto.getSuccessResponseDto(true);
    }

}
